package www.way2inbox.adapter;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.StrictMode;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.way2sms.way2inbox.R;

import www.way2inbox.common.PrintMsg;
import www.way2inbox.data.SectionItem;
import www.way2inbox.data.item;
import www.way2inbox.data.smsfetch;

/**
 * Created by sys1031 on 13/12/14.
 */
import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class SMSchatlistadapter extends ArrayAdapter<item> {

    private Context context;
    private ArrayList<item> items;
    private LayoutInflater vi;

    public SMSchatlistadapter(Context context,ArrayList<item> items) {
        super(context,0, items);
        this.context = context;
        this.items = items;
        vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        final item i = items.get(position);
        if (i != null) {
            if(i.isSection()){
                SectionItem si = (SectionItem)i;
                v = vi.inflate(R.layout.adapter_date_section, null);

                v.setOnClickListener(null);
                v.setOnLongClickListener(null);
                v.setLongClickable(false);

                final TextView Headerdate= (TextView) v.findViewById(R.id.Headerdate);
                Headerdate.setText(si.getTitle());

            }else{
                smsfetch model = (smsfetch)i;
                v = vi.inflate(R.layout.adapter_chat_in_out, null);

                final TextView Chat_in_msg = (TextView) v.findViewById(R.id.Chat_in_msg);
                final TextView Chat_out_msg = (TextView) v.findViewById(R.id.Chat_out_msg);
                final TextView Chat_in_time=(TextView)v.findViewById(R.id.Chat_In_times);
                final TextView Chat_out_time=(TextView)v.findViewById(R.id.Chat_Out_times);
                final TextView Chat_in_Fromtype=(TextView)v.findViewById(R.id.Chat_In_FromType);
                final TextView Chat_out_Fromtype=(TextView)v.findViewById(R.id.Chat_out_FromType);
                final RelativeLayout Chat_in = (RelativeLayout) v.findViewById(R.id.Chat_in);
                final RelativeLayout Chat_out = (RelativeLayout) v.findViewById(R.id.Chat_out);

Log.e("CHAT ADAPTER",model.getFolderName()+"--"+model.getMsg()+"---"+model.get_subject());

                if (model.getFolderName().contentEquals("2")) {
                    Chat_in.setVisibility(View.VISIBLE);
                    Chat_out.setVisibility(View.GONE);
                    Chat_in_msg.setText(model.getMsg());
//
                    Chat_in_time.setText(getTime(Long.valueOf(model.getTime())));
//                    Chat_in_Fromtype.setText("SMS");
                    System.out.println("sent");


                    //SETUP FOR FROM SIM/WAY2SMS TO TEXT FIELD..

                    if(model.get_subject()!=null) {
                        if (model.get_subject().equalsIgnoreCase("Way2SMS")) {
                            Chat_in_Fromtype.setText("Way2SMS");
                        } else {
                            Chat_in_Fromtype.setText("SMS");
                        }
                    }
                } else if (model.getFolderName().equals("1")) {

                    Chat_in.setVisibility(View.GONE);
                    Chat_out.setVisibility(View.VISIBLE);
                    Chat_out_msg.setText(model.getMsg());
//
                    Chat_out_time.setText(getTime(Long.valueOf(model.getTime())));
                    Chat_out_Fromtype.setText("");
                    //System.out.println("inbox   "+model.getMsg());
                }
            }
        }
        return v;
    }


    String res;
    private String getTime(Long currentDateTime) {
        try {
//                long currentDateTime = System.currentTimeMillis();

            //creating Date from millisecond
            Date currentDate = new Date(currentDateTime);

            //printing value of Date
            System.out.println("current Date: " + currentDate);

            DateFormat df = new SimpleDateFormat("HH:mm a");

            //formatted value of current Date
            System.out.println("Milliseconds to Date: " + df.format(currentDate));

            //Converting milliseconds to Date using Calendar
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(currentDateTime);
            System.out.println("Milliseconds to Date using Calendar:"
                    + df.format(cal.getTime()));

            //copying one Date's value into another Date in Java
            Date now = new Date();
            Date copiedDate = new Date(now.getTime());
            res=df.format(cal.getTime());

            System.out.println("original Date: " + df.format(now));


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return res;
    }

}

